已知有十位顾客及其到达银行的时刻与服务所需的时间,通过编程该离散事件的仿真:
通过输入十位顾客及每位顾客对应的到达银行时刻与服务所需的时间,编程计算得出系统中各变量变化过程。
程序主体
1.main.py
保存 主程序功能代码
main
这个文件启动tools.py
保存 功能函数main.py
主程序代码
import tools
while True:
tools.show_menu()
# TODO 显示仿真系统的功能
action_str = input("请选择希望执行的操作:")
print("您选择的操作是【%s】"%action_str)
# 1,2,3 仿真系统的功能
if action_str in ['1','2',"3"]:
# 1:输入数据信息;
if action_str == '1':
tools.new_num()
# 2:显示全部信息;
elif action_str == '2':
tools.show_all()
# 3:开始仿真
elif action_str == "3":
tools.begin_()
# 0:退出系统
elif action_str == "0":
print('欢迎再次使用系统')
break
# 其他输入则提示输入错误
else:
print('您输入的不正确,请重新选择')
tools.py
功能函数代码import matplotlib.pyplot as plt
import numpy as np
# 记录所有的顾客字典的列表
num_list = []
def show_menu():
"""显示菜单"""
print("*" *80)
print("欢迎使用【银行系统仿真】V 1.0")
print("")
print("1.输入数据信息")
print("2.显示全部信息")
print("3.开始仿真")
print("")
print("0.退出系统")
print("*" * 80)
def new_num():
"""输入数据信息"""
print("*" * 80)
print("输入数据信息")
# 1. 提示输入仿真所需的数据
serial_number = input("请输入顾客号:")
arrive_time = input("请输入到达时间:")
service_hours = input("请输入服务时间:")
# 2. 使用输入的信息建立一个数据字典
num_dict = {"顾客号":serial_number,
"到达时刻":arrive_time,
"服务时间":service_hours}
# 3. 将顾客字典添加到列表中
num_list.append(num_dict)
print(num_list)
# 4. 提示用户添加成功
print("顾客号%s的信息添加成功!" % serial_number)
def show_all():
"""显示全部信息"""
print("*" * 80)
print("显示全部信息")
# 判断是否存在记录,如果没有,提示用户并且返回
if len(num_list) == 0:
print("抱歉,没有检查到该记录")
return
# 打印表头
for name in ["顾客号","到达时刻","服务时间"]:
print(name,end="\t\t")
print("") # for循环内部所有内容输出后的换行
# 打印分隔线
print("="*80)
# 遍历顾客列表
for num_dict in num_list:
d = str(num_dict["到达时刻"])
print("%s\t\t\t%s\t\t\t%s"%(num_dict["顾客号"],
d.zfill(4),
num_dict["服务时间"]))
def begin_():
"""开始仿真"""
daoda_list = [0.0]
fuwu = []
for card_dict in num_list:
daoda_list.append(float(card_dict["到达时刻"]))
fuwu.append(float(card_dict["服务时间"]))
print("*" * 80)
print("开始仿真")
print(" ")
# 服务开始时间
fuwukaishi_list = []
# 离开时间
likai_list = []
# 计算到达时间间隔
daodashije_list = [] # 到达时间间隔
for i in range(len(daoda_list) - 1):
das = round(float(daoda_list[i + 1]) - float(daoda_list[i]), 2)
daodashije_list.append(das)
# 计算服务开始时间与离开时间
for i in range(len(daoda_list) - 1):
if i == 0:
f = daoda_list[1]
fuwukaishi_list.append(f) # 服务开始时间
l = fuwukaishi_list[i] + fuwu[i] # 离开时间
likai_list.append(l)
elif i >= 1:
if daoda_list[i + 1] > likai_list[i - 1]:
fuwukaishi_list.append(round(daoda_list[i + 1], 2))
likai_list.append(round(fuwukaishi_list[i] + fuwu[i], 2))
else:
fuwukaishi_list.append(round(likai_list[i - 1], 2))
likai_list.append(round(fuwukaishi_list[i] + fuwu[i], 2))
# 时钟时间 = 到达时间与离开时间的升序排列
clock_list = []
clock_list.extend(daoda_list)
clock_list.extend(likai_list)
clock_list.sort()
# 定义一些系统变量
gukehao_list = [] # 顾客号列表
shijian_list = [] # 事件类型列表
duilie_list = [] # 队列长度列表
hangshu_list = [] # 在银行中人数列表
zhuangtai_list = [] # 服务台状态
kongshi_list = [] # 空闲时间
hc_list = []
for i in range(len(clock_list)):
if i == 0:
gukehao_list.append("-")
shijian_list.append("开始营业")
duilie_list.append(0)
hangshu_list.append(0)
zhuangtai_list.append("空闲")
kongshi_list.append(" ")
else:
if clock_list[i] in daoda_list:
for j in range(len(daoda_list)):
if daoda_list[j] == clock_list[i]:
gukehao_list.append(j)
shijian_list.append("顾客到达")
hc_list.append(1)
hangshu_list.append(len(hc_list))
duilie_list.append(len(hc_list) - 1)
zhuangtai_list.append("忙碌")
elif clock_list[i] in likai_list:
for k in range(len(likai_list)):
if likai_list[k] == clock_list[i]:
gukehao_list.append(k + 1)
shijian_list.append("顾客离开")
hc_list.pop()
if len(hc_list) > 0:
hangshu_list.append(len(hc_list))
duilie_list.append(len(hc_list) - 1)
zhuangtai_list.append("忙碌")
else:
hangshu_list.append(len(hc_list))
duilie_list.append(len(hc_list))
zhuangtai_list.append("空闲")
# 计算在队列中的时间与在银行系统中的时间
duishi_list = [] # 在队列中的时间
hangshi_list = [] # 在银行系统中的时间
for i in range(len(daoda_list) - 1):
duishi_list.append(round(fuwukaishi_list[i] - daoda_list[i + 1], 2))
hangshi_list.append(round(likai_list[i] - daoda_list[i + 1], 2))
# 1. 输出仿真过程
chu_list1 = []
for i in range(len(clock_list)):
clock = clock_list[i]
gukehao = gukehao_list[i]
shijian = shijian_list[i]
duilie = duilie_list[i]
hangshu = hangshu_list[i]
zhuangtai = zhuangtai_list[i]
# 使用仿真数据信息建立一个数据字典
chu_dict1 = {"时钟时间": clock,
"顾客号": gukehao,
"事件类型": shijian,
"队列长度": duilie,
"系统中的人数": hangshu,
"服务台状态": zhuangtai}
# 将顾客字典添加到列表中
chu_list1.append(chu_dict1)
print("输出银行系统仿真过程:")
print("-"*80)
# 打印表头
for name in ["时钟时间","顾客号", "事件类型", "队列长度","系统中的人数","服务台状态"]:
print(name, end="\t\t")
print("") # for循环内部所有内容输出后的换行
# 打印分隔线
print("=" * 80)
# 遍历顾客列表
for chu_dict1 in chu_list1:
s = str(chu_dict1["时钟时间"])
print("%s\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s\t\t\t%s" % (s.zfill(4),
chu_dict1["顾客号"],
chu_dict1["事件类型"],
chu_dict1["队列长度"],
chu_dict1["系统中的人数"],
chu_dict1["服务台状态"]))
print("")
# 2.输出仿真统计数据
chu_list2 = []
for i in range(len(num_list)):
num = i + 1
daoda= daoda_list[i+1]
fuwukaishi = fuwukaishi_list[i]
likai = likai_list[i]
duishi = duishi_list[i]
hangshi = hangshi_list[i]
# 使用仿真数据信息建立一个数据字典
chu_dict2 = {"顾客": num,
"到达时刻": daoda,
"开始服务时间": fuwukaishi,
"离开时间": likai,
"在队列中的时间": duishi,
"在银行中逗留时间": hangshi}
# 将顾客字典添加到列表中
chu_list2.append(chu_dict2)
print("输出仿真统计数据:")
print("-" * 80)
# 打印表头
for name in ["顾客", "到达时刻", "开始服务时间","离开时间","在队列中的时间","在银行中逗留时间"]:
print(name, end="\t\t")
print("") # for循环内部所有内容输出后的换行
# 打印分隔线
print("=" * 80)
# 遍历顾客列表
for chu_dict2 in chu_list2:
d = str(chu_dict2["到达时刻"])
k = str(chu_dict2["开始服务时间"])
la = str(chu_dict2["离开时间"])
print("%s\t\t%s\t\t\0%s\t\t\t%s\t\t%s\t\t\t\t\t\0%s" % (chu_dict2["顾客"],
d.zfill(4),
k.zfill(4),
la.zfill(4),
chu_dict2["在队列中的时间"],
chu_dict2["在银行中逗留时间"]))
# 3. 系统变量变化情况分析
i_list = []
i = 0
while i <= 40:
i += 2
i_list.append(i)
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams["axes.unicode_minus"] = False
c = np.mean(hangshu_list)
plt.bar(i_list, hangshu_list, width=2,color='white',edgecolor="black",hatch='')
plt.title("系统变量变化情况")
plt.xlabel("时钟时间")
plt.ylabel("系统中的人数")
plt.axhline(y=c, color="blue")
plt.show()
#服务员状态
plt.bar(i_list, zhuangtai_list, width=2,color='blue',edgecolor="black",hatch='')
plt.title("系统变量变化情况")
plt.xlabel("时钟时间")
plt.ylabel("服务员状态")
plt.show()
# 4. 统计结构分析
hao_list = []
for x in range(10):
hao_list.append(x + 1)
plt.bar(hao_list, duishi_list, width=0.5,color='white', edgecolor="black", hatch='')
plt.title("系统变量变化情况")
plt.xlabel("顾客在队列中等待时间的变化情况")
plt.ylabel("队列中等待时间(min)")
plt.show()
print("仿真结束")
程序运行截图如下:
银行系统的仿真过程:
代码运行截图:
将程序计算得出的结果填入下表:
时钟时间(min) | 顾客号 | 事件类型 | 队列长度 | 系统中人数 | 服务台状态 | 空闲时间 |
---|---|---|---|---|---|---|
00.0 | — | 开始营业 | 0 | 0 | 空闲 | |
03.2 | 1 | 顾客到达 | 0 | 1 | 忙碌 | 3.2 |
07.0 | 1 | 顾客离开 | 0 | 0 | 空闲 | |
10.9 | 2 | 顾客到达 | 0 | 1 | 忙碌 | 3.9 |
13.2 | 3 | 顾客到达 | 1 | 2 | 忙碌 | |
14.4 | 2 | 顾客离开 | 0 | 1 | 忙碌 | |
14.8 | 4 | 顾客到达 | 1 | 2 | 忙碌 | |
17.7 | 5 | 顾客到达 | 2 | 3 | 忙碌 | |
18.6 | 3 | 顾客离开 | 1 | 2 | 忙碌 | |
19.8 | 6 | 顾客到达 | 2 | 3 | 忙碌 | |
21.5 | 7 | 顾客到达 | 3 | 4 | 忙碌 | |
21.7 | 4 | 顾客离开 | 2 | 3 | 忙碌 | |
24.1 | 5 | 顾客离开 | 1 | 2 | 忙碌 | |
26.3 | 8 | 顾客到达 | 2 | 3 | 忙碌 | |
28.4 | 6 | 顾客离开 | 1 | 2 | 忙碌 | |
31.1 | 7 | 顾客离开 | 0 | 1 | 忙碌 | |
32.1 | 9 | 顾客到达 | 1 | 2 | 忙碌 | |
33.2 | 8 | 顾客离开 | 0 | 1 | 忙碌 | |
35.7 | 9 | 顾客离开 | 0 | 0 | 空闲 | |
36.6 | 10 | 顾客到达 | 0 | 1 | 忙碌 | 0.9 |
40.0 | 10 | 顾客离开 | 0 | 0 | 空闲 |
输出仿真统计数据:
代码运行截图:
将程序计算得出的结果填入下表:
顾客(1) | 到达时刻(2) | 开始服务时间(3) | 离开时间(4) | 在列队中的时间 (5) = (3) - (2) |
在银行中逗留时间 (6) = (4) - (2) |
---|---|---|---|---|---|
1 | 3.2 | 3.2 | 7.0 | 0.0 | 3.8 |
2 | 10.9 | 10.9 | 14.4 | 0.0 | 3.5 |
3 | 13.2 | 14.4 | 18.6 | 1.2 | 5.4 |
4 | 14.8 | 18.6 | 21.7 | 3.8 | 6.9 |
5 | 17.7 | 21.7 | 24.1 | 4.0 | 6.4 |
6 | 19.8 | 24.1 | 28.4 | 4.3 | 8.6 |
7 | 21.5 | 28.4 | 31.1 | 6.9 | 9.6 |
8 | 26.3 | 31.1 | 33.2 | 4.8 | 6.9 |
9 | 32.1 | 33.2 | 35.7 | 1.1 | 3.6 |
10 | 36.6 | 36.6 | 40.0 | 0.0 | 3.4 |
系统变量变化分析:
代码运行输出的结果: